package jmine.tec.utils.reflection;

import java.lang.reflect.Method;

/**
 * @author takeshi
 * @deprecated
 */
@Deprecated
public class CachedReflectionResult {

    private final Class<?> type;

    /**
     * C'tor
     * 
     * @param type the type
     */
    public CachedReflectionResult(final Class<?> type) {
        super();
        this.type = type;
    }

    /**
     * Creates a new instance
     * 
     * @return Object
     */
    public Object newInstance() {
        try {
            return this.type.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(e.getCause());
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e.getCause());
        }
    }

    /**
     * @param declaringClass the declaring class
     * @param methodName the method name
     * @return Method
     */
    public static Method findEmptyMethod(final Class<?> declaringClass, final String methodName) {
        try {
            return declaringClass.getMethod(methodName);
        } catch (final NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param method Method
     * @return String
     */
    public static String parsePropertyName(final Method method) {
        if (method.getParameterTypes().length > 0) {
            throw new IllegalArgumentException("Method " + method + " is not a property!");
        }
        final String methodName = method.getName();
        String name = extractPropertyName(methodName, "get");
        if (name == null) {
            name = extractPropertyName(methodName, "is");
        }
        if (name == null) {
            throw new IllegalArgumentException("Method " + method + " is not a property!");
        }
        return name;
    }

    /**
     * @param methodName String nome do metodo
     * @param prefix String prefixo
     * @return String null ou o nome da propriedade comecando com o prefixo dado
     */
    private static String extractPropertyName(final String methodName, final String prefix) {
        if (!methodName.startsWith(prefix)) {
            return null;
        }
        final String name = methodName.substring(prefix.length());
        return Character.toLowerCase(name.charAt(0)) + name.substring(1);
    }

}
